| Conditions | 2 |
| Paths | 2 |
| Total Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | 'use strict'; |
||
| 5 | MongoClient.connect('mongodb://localhost:27017/JokesApp', (err, db) => {
|
||
| 6 | if (err) |
||
| 7 | return console.log('unable to connect MongoDB' + err);
|
||
| 8 | console.log('Connected to MongoDB Server');
|
||
| 9 | |||
| 10 | // creating a new collection, a table like structure |
||
| 11 | db.collection('Jokes').insertOne({
|
||
| 12 | joke : 'Sample joke 2', |
||
| 13 | likes : 0, |
||
| 14 | }, (err, result) => {
|
||
| 15 | if (err) |
||
| 16 | return console.log('unable to insert data' + err);
|
||
| 17 | |||
| 18 | console.log(JSON.stringify(result.ops, undefined, 2)); |
||
| 19 | }); |
||
| 20 | |||
| 21 | // one more users collection |
||
| 22 | db.collection('Users').insertOne({
|
||
| 23 | name : 'Ashok Dey', |
||
| 24 | age : 22, |
||
| 25 | location : 'Delhi' |
||
| 26 | }, (err, result) => {
|
||
| 27 | if (err) |
||
| 28 | return console.log('Failed to insert data into the user collection');
|
||
| 29 | |||
| 30 | console.log(JSON.stringify(result.ops, undefined, 2)); |
||
| 31 | }); |
||
| 32 | |||
| 33 | // close the database |
||
| 34 | db.close(); |
||
| 35 | }); |